package com.example.warehousems.event.listener;

import com.example.warehousems.component.UserInfo;
import com.example.warehousems.event.event.InOutEvent;
import com.example.warehousems.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class MessageEventListener implements ApplicationListener<InOutEvent> {
    @Autowired
    private MessageService messageService;
    @Override
    public void onApplicationEvent(InOutEvent event) {
        var userId = Integer.valueOf(UserInfo.get("id"));
        Integer type = 0;
        if (event.getId() == 1) type = 1;
        else if (event.getId() == 2) type = 2;
        messageService.addMessage(userId, event.getMessage(), type);
        System.out.println(event);
    }
}
